<template>
	<view class="search">
		<music-head title="搜索" :isShow="true" :bgblack="true"></music-head>
		
		<!-- 搜索框 -->
		<view class="index_search">
			<view class="search_content">
				<view class="iconfont icon-sousuo search_icon"></view>
				<input type="text" placeholder="搜索歌曲" v-model="searchKey" @confirm="handleToSearch(searchKey)" @input="suggestInput"/>
			</view>
		</view>
		
		<view class="container">
			<scroll-view scroll-y="true">
				<block v-if='searchType == 1'>
					<!-- 历史记录 -->
					<view class="history">
						<view class="history-head">
							<view class="history-text">历史记录</view>
							<view class="iconfont icon-shanchu history-icon" @tap="handleTodelHis()"></view>
						</view>
						<view class="history-content">
							<view class="history-item" v-for="(item,index) in historySearch" :key="index" @tap="handleToInput(item)">
								{{ item }}
							</view>
						</view>
					</view>
					
				<!-- 热搜榜 -->
				<view class="hotSearch">
					<view class="hotSearch-head">
						热搜榜
					</view>
					<view class="hotSearch-item" v-for="(item,index) in hotSearch" :key='index' @tap="handleToInput(item.searchWord)">
						<view class="hotSearch-index">{{index + 1}}</view>
						<view class="hotSearch-content">
							<view class="hotSearch-music">
								<view class="music-title">
									<view class="title-text">{{ item.searchWord }}</view>
									<view v-if="item.iconUrl == 'https://p1.music.126.net/PtgUJbcvDAY0HKWpmOB2HA==/109951163967988470.png'" class="iconfont icon-shangjiantou music-icon"></view>
									<view v-if="item.iconType == 1" class="iconfont icon-hot music-icon"></view>
								</view>
								<view class="music-content">{{ item.content }}</view>
							</view>
							<view class="hotSearch-num">{{ item.score }}</view>
						</view>
					</view>
				</view>
				</block>
				
				<!-- 歌曲列表显示 -->
				<block v-else-if="searchType == 2">
					<view class="paly-list">
							<view class="play-music-item" v-for="(item,index) in listMusic" :key="item.id" @tap="handleToDetail(item.id)">
								<view class="paly-music-index"><view>{{index + 1}}</view></view>
								<view class="paly-music-content">
									<view class="music-title">{{ item.name }}</view>
									<view class="music-singer">
										<view class="singer-name">
											{{item.artists[0].name}} - {{ item.name }}
										</view>
									</view>
								</view>
								<view class="iconfont icon-24gl-playCircle paly-item-icon">
								</view>
							</view>
						</view>
				</block>
				
				<!-- 搜索建议 -->
				<block v-else-if="searchType == 3">
					<view class="suggestContent">
						<view class="suggestHead">
							搜索'吉他'
						</view>
						<view class="suggestItem" v-for="(item,index) in suggestList" :key="item.id">
							<view class="iconfont icon-sousuo suggestIcon"></view>
							<view class="suggestTest" @tap="handleToInput(item.name)">
								{{ item.name }}
							</view>
						</view>
					</view>
				</block>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import { hotSearch, suggestSearch, keywordsSearch } from '../../common/api.js'
	export default {
		data() {
			return {
				hotSearch: [],
				searchKey: '',
				historySearch: [],
				searchType: 1,  //控制搜索显示
				listMusic: [],
				suggestList: []
			}
		},
		onLoad(){
			this.getHotSearch()
			uni.getStorage({
			    key: 'historySearch',
			    success: (res)=>{
					this.historySearch = res.data
			    }
			});
		},
		methods: {
			//获取热搜榜
			getHotSearch(){
				hotSearch().then((res)=>{
					if(res.data.code == 200){
						this.hotSearch = res.data.data
					}
				})
			},
			//热搜榜进行响应式
			handleToInput(searchWord){
				this.searchKey = searchWord
			},
			
			//输入框回车搜索（添加到历史记录）
			handleToSearch(searchValue){
				if(!this.historySearch){
					return 
				}else{
					this.historySearch.unshift(searchValue)
					this.historySearch = [...new Set(this.historySearch)]  //去重
					if(this.historySearch.length > 8){
						this.historySearch = 8
					}
					uni.setStorage({
					    key: 'historySearch',
					    data: this.historySearch
					});
					this.searchKey = ''
					this.getMusic(searchValue)
					this.searchType = 2
				}
			},
			//删除历史记录
			handleTodelHis(){
				uni.removeStorage({
				    key: 'historySearch'
				});
				this.historySearch = []
			},
			//搜索歌曲
			getMusic(keyWord){
				keywordsSearch(keyWord).then((res)=>{
					this.listMusic = res.data.result.songs
				})
			},
			//跳转至详情页
			handleToDetail(id){
				uni.navigateTo({
					url: "../detail/detail?detailId=" + id
				})
			},
			//搜索建议
			suggestInput(event){
				this.searchType = 3
				if(!event.detail.value || event.detail.value == ' '){
					this.searchType = 1
				}else{
					suggestSearch(this.searchKey).then((res)=>{
						this.suggestList = res.data.result.albums
					})
				}
			}
		}
	}
</script>

<style lang="scss">
	.index_search{
			width: 100%;
			display: flex;
			align-items:center;
			justify-content: center;
			height: 105rpx;
			
			.search_content{
				display: flex;
				align-items: center;
				height: 70rpx;
				width: 100%;
				margin: 0rpx 32rpx;
				border-radius: 20px;
				background-color: #f7f7f7;
				
				.search_icon{
					margin: 0 20rpx;
					font-size: 35rpx;
					color: rgb(181, 181, 181);
				}
				input{
					width: 100%;
				}
			}
		}
		
	.container{
		width: 100%;
		height: calc(100vh - 255rpx);
		overflow: hidden;
		
		scroll-view{
				height: 100%;
		}
	}
	//历史记录
	.history{
		margin: 0 32rpx;
		display: flex;
		flex-direction: column;
		
		.history-head{
			height: 56rpx;
			display: flex;
			justify-content: space-between;
			
			.history-text{
				font-size: 27rpx;
			}
			
			.history-icon{
				font-size: 29rpx;
			}
		}
		
	.history-content{
		display: flex;
		flex-wrap: wrap;
		.history-item{
			height: 60rpx;
			line-height: 60rpx;
			padding: 5rpx 24rpx;
			text-align: center;
			font-size: 28rpx;
			border-radius: 20rpx;
			background-color: #f7f7f7;
			margin: 10rpx 20rpx;
		}
		}
	}
	
	//热搜榜
	.hotSearch{
		display: flex;
		flex-direction: column;
		margin: 10rpx 32rpx;
		
		.hotSearch-head{
			height: 60rpx;
			font-size: 30rpx;
		}
		
		.hotSearch-item{
			height: 102rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			margin-bottom: 20rpx;
			.hotSearch-index{
				width: 46rpx;
				height: 46rpx;
				display: flex;
				justify-content: start;
				align-items: center;
				color: red;
				font-size: 28rpx;
			}
			
			.hotSearch-content{
				width: 100%;
				height: 77rpx;
				display: flex;
				justify-content: space-between;
				margin-left: 10rpx;
				align-items: center;
				
				.hotSearch-music{
					
					.music-title{
						display: flex;
						align-items: center;
						height: 38rpx;
						margin-bottom: 10rpx;
						.title-text{
							font-size: 34rpx;
							margin-right: 12rpx;
						}
						.music-icon{
							font-size: 28rpx;
							color: red;
						}
					}
					
					.music-content{
						width: 445rpx;
						font-size: 25rpx;
						color: #c5c5c5;
						white-space: nowrap;
						text-overflow: ellipsis;
						overflow: hidden;
					}
				}
				
				.hotSearch-num{
					color: #c5c5c5;
				}
			}
		}
	}
	//播放列表
	.paly-list{
		width: 100%;
		display: flex;
		flex-direction: column;
		
		.play-music-item{
			width: 100%;
			display: flex;
			align-items: center;
			height: 111rpx;
			position: relative;
			
			.paly-music-index{
				width: 100rpx;
				height: 100rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				
			}
			
			.paly-music-content{
				display: flex;
				flex-direction: column;
				
				.music-title{
					font-size: 32rpx;
					margin-bottom: 10rpx;
					width: 570rpx;
					white-space: nowrap;
					overflow: hidden;
					text-overflow: ellipsis;
				}
				
				.music-singer{
					display: flex;
					align-items: center;
					
					.music-type{
						background-color: black;
						line-height: 22rpx;
						flex-shrink: 0;
						display: flex;
						align-items: center;
						margin-right: 10rpx;
						
						image{
							width: 33rpx;
							height: 22rpx;
						}
					}
					.singer-name{
						font-size: 23rpx;
						color: rgb(168, 168, 168);
						width: 500rpx;
						white-space: nowrap;
						overflow: hidden;
						text-overflow: ellipsis;
					}
				}
			}
			
			.paly-item-icon{
				position: absolute;
				right: 30rpx;
				top: 50%;
				transform: translateY(-50%);
				font-size: 48rpx;
			}
		}
	}
	
	//搜索建议
	.suggestContent{
		width: 100%;
		display: flex;
		flex-direction: column;
		margin: 55rpx 56rpx;
		.suggestHead{
			height: 47rpx;
			color: #4c7db1;
			font-size: 31rpx;
		}
		.suggestItem{
			height: 57rpx;
			display: flex;
			align-items: center;
			margin-top: 44rpx;
			.suggestIcon{
				font-size: 35rpx;
			}
			.suggestTest{
				width: 100%;
				font-size: 32rpx;
				margin-left: 18rpx;
			}
		}
	}
</style>
